package sites.manager.admin.site;

import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import sites.manager.common.kit.Ret;
import sites.manager.common.kit.SiteInfo;
import sites.manager.common.model.Site;

import java.util.Date;

/**
 * 站点管理
 */
public class SiteService {

    private Site dao = new Site().dao();

    /**
     * 分页查询
     *
     * @param siteName
     * @param siteSign
     * @param page
     * @param limit
     * @return
     */
    public Ret getSites(String siteName, String siteSign, int page, int limit){
        Ret ret = Ret.ok();

        Kv kv = Kv.by("siteSign", siteSign).set("siteName", "%"+siteName+"%");
        Page<Site> sitePage = dao.template("site.getSites", kv).paginate(page, limit);

        //组装结果
        ret.setCode("0");
        ret.setData(sitePage.getList());
        ret.setCount(sitePage.getTotalRow());

        return ret;
    }

    /**
     * 保存站点信息
     *
     * @param site
     * @return
     */
    public Ret saveSite(Site site) {
        Ret ret = Ret.ok();

        Site temp = getBySiteSign(site.getSiteSign());
        if(temp != null){
            ret.setFail();
            ret.setMsg("站点标示【"+site.getSiteSign()+"】已被占用，请设定其他值！");
            return ret;
        }

        site.setStatus(SiteInfo.statusNormal);
        site.setCreateTime(new Date());
        boolean flag = site.save();
        if(!flag) {
            ret.setFail();
            ret.setMsg("保存失败");
        }
        return ret;
    }

    /**
     * 根据站点标示查询站点信息
     *
     * @param siteSign
     * @return
     */
    public Site getBySiteSign(String siteSign){
        if(StrKit.isBlank(siteSign)){
            return null;
        }

        String sql = dao.getSql("site.getBySiteSign");
        return dao.findFirst(sql, siteSign);
    }

    /**
     * 根据id查询站点信息
     *
     * @param id
     * @return
     */
    public Site getById(int id){
        return (Site) dao.findById(id);
    }

    /**
     * 更新站点信息
     *
     * @param site
     * @return
     */
    public Ret updateSite(Site site){
        Ret ret = Ret.create();
        Site temp = getById(site.getId());
        temp.setSiteName(site.getSiteName());
        temp.setSiteUrl(site.getSiteUrl());
        temp.setImgMaxSize(site.getImgMaxSize());
        temp.setFileMaxSize(site.getFileMaxSize());
        temp.setVideoMaxSize(site.getVideoMaxSize());
        temp.setImgSuffix(site.getImgSuffix());
        temp.setFileSuffix(site.getFileSuffix());
        temp.setVideoSuffix(site.getVideoSuffix());
        temp.setAccessLog(site.getAccessLog());
        temp.setSiteTitle(site.getSiteTitle());
        temp.setSiteKeys(site.getSiteKeys());
        temp.setSiteDes(site.getSiteDes());
        temp.setUpdateTime(new Date());
        boolean flag = temp.update();
        if(flag){
            ret.setOk();
            ret.set(SiteInfo.msgKey, "更新成功");
        } else {
            ret.setFail();
            ret.set(SiteInfo.msgKey, SiteInfo.serverError);
        }
        return ret;
    }

    /**
     * 删除站点信息，支持批量删除
     *
     * @param ids
     * @return
     */
    public Ret deleteSite(String ids){
        Ret ret = Ret.create();

        String[] idArray = ids.split(",");

        int num = 0;
        for(int i=0,j=idArray.length; i<j; i++){
            dao.deleteById(idArray[i]);
            num++;
        }

        if(num > 0){
            ret.setOk();
            ret.setMsg("删除"+num+"行");
        } else {
            ret.setFail();
            ret.setMsg("操作失败");
        }
        return ret;
    }

    /**
     * 初始化站点信息
     */
    public void initSiteInfo(){
        //在siteConfig.txt中的配置
        SiteInfo.uploadPath = PropKit.get("uploadPath");
        SiteInfo.siteUrl = PropKit.get("siteUrl");
        SiteInfo.siteSign = PropKit.get("siteSign");

        //在数据库中的配置
        String sql = dao.getSql("site.getBySiteSign");
        Site site = dao.findFirst(sql, SiteInfo.siteSign);
        if(site != null){//不为空说明数据库中设置了主站点
            SiteInfo.siteId = site.getId();
            SiteInfo.siteName = site.getSiteName();
            SiteInfo.siteSign = site.getSiteSign();
            SiteInfo.siteDes = site.getSiteDes();

            SiteInfo.siteTitle = site.getSiteTitle();
            SiteInfo.siteKeys = site.getSiteKeys();
            SiteInfo.siteDes = site.getSiteDes();

            SiteInfo.imgMaxSize = site.getImgMaxSize();
            SiteInfo.fileMaxSize = site.getFileMaxSize();
            SiteInfo.videoMaxSize = site.getVideoMaxSize();

            SiteInfo.imgSuffix = site.getImgSuffix();
            SiteInfo.fileSuffix = site.getFileSuffix();
            SiteInfo.videoSuffix = site.getVideoSuffix();

            SiteInfo.accessLog = site.getAccessLog();
        }
    }

}
